import {createRouter, createWebHashHistory} from 'vue-router'
import {ElMessage} from "element-plus";
import EventEmitter from "@/utils/event.js";

const backendRoutes = [
    {
        path: '/',
        name: 'Login',
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/dashboard',
        name: 'DashBoard',
        component: () => import('@/views/DashBoard.vue'),
        children: [
            {
                path: '/index',
                name: 'Index',
                component: () => import('@/views/Index.vue'),
            },
            {
                path: '/link',
                name: 'Link',
                component: () => import('@/views/Link.vue'),
            },
            {
                path: '/access',
                name: 'Access',
                component: () => import('@/views/Access.vue'),
            },
            {
                path: '/profile',
                name: 'Profile',
                component: () => import('@/views/Profile.vue'),
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(process.env.BASE_URL),
    routes: backendRoutes
})

EventEmitter.on('NO_LOGIN', (res) => {
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    ElMessage.error("登录信息过期或未登录")
    router.push({name: 'Login'})
});

export default router
